"""
快速使用示例
演示如何快速生成各种信号
"""

from signal_generator import *
import matplotlib.pyplot as plt

# 配置中文字体
plt.rcParams['font.sans-serif'] = ['SimHei', 'Microsoft YaHei', 'Arial Unicode MS']
plt.rcParams['axes.unicode_minus'] = False


def example_1_simple_harmonic():
    """示例1: 简单谐波信号"""
    print("示例1: 生成简单谐波信号")
    
    params = SignalParams(fs=10000, duration=1.0)
    gen = HarmonicGenerator(params)
    signal, info = gen.generate(fundamental_freq=50.0, num_harmonics=3)
    
    print(f"  基频: {info['fundamental_freq']} Hz")
    print(f"  包含谐波: {info['frequencies']}")
    return signal


def example_2_am_signal():
    """示例2: 调幅信号（模拟齿轮故障）"""
    print("\n示例2: 生成调幅信号（模拟齿轮故障）")
    
    params = SignalParams(fs=10000, duration=1.0)
    gen = ModulationGenerator(params)
    
    rotation_freq = 30.0  # 转速 30Hz
    num_teeth = 20        # 齿数
    mesh_freq = rotation_freq * num_teeth  # 啮合频率
    
    signal, info = gen.generate_am(
        carrier_freq=mesh_freq,
        modulation_freq=rotation_freq,
        modulation_index=0.7
    )
    
    print(f"  转速: {rotation_freq} Hz")
    print(f"  齿数: {num_teeth}")
    print(f"  啮合频率: {mesh_freq} Hz")
    print(f"  边带频率: {info['sideband_freqs']}")
    return signal


def example_3_bearing_fault():
    """示例3: 轴承故障信号"""
    print("\n示例3: 生成轴承故障信号")
    
    params = SignalParams(fs=20000, duration=0.5)
    gen = BearingFaultGenerator(params)
    
    signal, info = gen.generate_outer_race_fault(
        rotation_freq=25.0,   # 1500 RPM
        num_balls=9
    )
    
    print(f"  转速: {info['rotation_freq']} Hz ({info['rotation_freq']*60:.0f} RPM)")
    print(f"  滚动体数: {info['num_balls']}")
    print(f"  BPFO: {info['BPFO']:.2f} Hz")
    return signal


def example_4_custom_composite():
    """示例4: 自定义叠加信号"""
    print("\n示例4: 自定义叠加多个信号")
    
    params = SignalParams(fs=10000, duration=1.0, noise_level=0.05)
    
    # 创建三个不同的信号
    gen1 = HarmonicGenerator(params)
    signal1, _ = gen1.generate(fundamental_freq=50.0, num_harmonics=2)
    
    gen2 = ModulationGenerator(params)
    signal2, _ = gen2.generate_am(carrier_freq=300.0, modulation_freq=25.0)
    
    gen3 = BearingFaultGenerator(params)
    signal3, _ = gen3.generate_outer_race_fault(rotation_freq=20.0)
    
    # 叠加
    composite_gen = CompositeSignalGenerator(params)
    combined = composite_gen.combine_signals(
        [signal1, signal2, signal3],
        weights=[1.0, 0.5, 1.2]
    )
    
    print(f"  已叠加3个信号")
    print(f"  权重: [1.0, 0.5, 1.2]")
    print(f"  包含: 谐波 + 调制 + 轴承故障")
    return combined


def example_5_batch_generation():
    """示例5: 批量生成不同参数的信号"""
    print("\n示例5: 批量生成不同转速的轴承故障信号")
    
    params = SignalParams(fs=20000, duration=0.3)
    gen = BearingFaultGenerator(params)
    
    # 生成5个不同转速的信号
    rotation_freqs = [15, 20, 25, 30, 35]  # Hz
    signals = []
    
    for rf in rotation_freqs:
        signal, info = gen.generate_outer_race_fault(rotation_freq=rf)
        signals.append(signal)
        print(f"  转速 {rf} Hz -> BPFO: {info['BPFO']:.2f} Hz")
    
    return signals


if __name__ == "__main__":
    print("="*60)
    print("仿真信号生成 - 快速示例")
    print("="*60)
    
    # 运行所有示例
    example_1_simple_harmonic()
    example_2_am_signal()
    example_3_bearing_fault()
    example_4_custom_composite()
    example_5_batch_generation()
    
    print("\n" + "="*60)
    print("示例运行完成！")
    print("="*60)
    print("\n提示:")
    print("  - 运行 'python demo.py' 可查看完整演示和生成图片")
    print("  - 查看 signal_generator.py 了解更多参数选项")
    print("  - 参考 README.md 了解详细使用方法")


